const Actions = require('./actions');
const DB = require('app/db.js');
import { Control } from 'react-keeper';
const { Toast } = SaltUI
import { NEW, RUNNING, TERMINATED, COMPLETED, CANCELED } from 'clientConfig/constants'
import { monStorage, Storage } from 'clientConfig/util/StoreData';
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },
    onFetch: function (params, cb) {
        let t = this;
        var changeType = "";
        //审批已完成的可以内容变更和撤销变更，审批中(未完成)-撤销变更
        DB.Approval.get_approval_detail({ process_id: params }).then((content) => {
            if (Storage.get('user.id') != content.originator_user_id) {
                Toast.show({
                    type: "error",
                    content: '对不起,您不是申请人',
                });
                return;
            }
            let approvalType = content.proj_category;
            let status = content.status;
            if (status == TERMINATED || status == CANCELED) {
                Toast.show({
                    type: "error",
                    content: '不能发起变更',
                });
                return;
            }else if (status == NEW || status == RUNNING) {
                changeType = "撤销申请"
            }else{
                changeType = "内容变更"
            }
            t.data.approvalType = approvalType;
            if (content.approvers instanceof Array)
                content.approvers = content.approvers.reduce((pre, cre) => {
                    if (pre.indexOf(cre) == -1) return pre.concat([cre])
                    else return pre
                }, [])
            if (content.approvers_id instanceof Array)
                content.approvers_id = content.approvers_id.reduce((pre, cre) => {
                    if (pre.indexOf(cre) == -1) return pre.concat([cre])
                    else return pre
                }, [])
            if (content.cc_list instanceof Array)
                content.cc_list = content.cc_list.reduce((pre, cre) => {
                    if (pre.indexOf(cre) == -1) return pre.concat([cre])
                    else return pre
                }, [])
            if (content.cc_list_id instanceof Array)
                content.cc_list_id = content.cc_list_id.reduce((pre, cre) => {
                    if (pre.indexOf(cre) == -1) return pre.concat([cre])
                    else return pre
                }, [])
            content.finish_time = '';
            if (approvalType == "software" || approvalType == "tenderplan") {
                t.data.pagedata = content;
                t.data.pagedata.changeType = changeType ? changeType : "内容变更";
                t.updateComponent();
                cb && cb();
            } else {
                //let approvalType = content[0].proj_category;
                //t.data.approvalType = approvalType;
                var pagedata = {};
                pagedata = content;
                // pagedata.folder_id = content.folder_id;
                pagedata.proName = { value: 0, text: content.proj_name };
                pagedata.proNum = content.proj_num;
                // pagedata.process_id = content.process_id;
                // pagedata.status = content.status;
                pagedata.changeType = changeType ? changeType : "内容变更";
                Object.assign(pagedata, content.form_component_values.nodelists[0]);
                if (!!pagedata.node_info[pagedata.node_info.length - 1].value) t.data.path_name = JSON.parse(pagedata.node_info[pagedata.node_info.length - 1].value)[0].selected_folder_pathname;
                t.data.pagedata = pagedata;
                t.updateComponent();
                cb && cb();
            }
        }).catch((error) => { // 失败 or 有异常被捕获
            if (error.errcode == 2) {
                Toast.show({
                    type: "error",
                    content: '错误的审批单号，请重试'
                })
            }

        });
    },
    onUpdata: function (params, cb) {
        DB.Approval.update_approval({
            process_id: params.process_id,
            //params.content
        }).then((content) => {// 成功
            content.success = true;
            if (!content.success) {
                Toast.show({
                    type: "error",
                    content: '表单提交失败',
                })
            } else {
                Control.go(-1)
            }
            t.updateComponent();
        }).catch((error) => { // 失败 or 有异常被捕获

        });
    },

    updateComponent: function () {

        this.trigger(this.data);
    }
});
